﻿using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL;
using ASPNETVN.PORTAL.Components.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI;
using System.Web;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Share;
using LMHTX.Modules.DIRECTORY.COOPERATIVE.Components;

namespace LMHTX.Modules.DIRECTORY.COOPERATIVE.Controls
{
    public partial class AreaManager : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnAdd;
            this.ButtonCancel = btnCancel;

            this.Update += AreaManager_Update;
            base.OnInit(e);
        }

        void AreaManager_Update(object sender, EventArgs e)
        {
            Components.Area area = new Components.Area();
            area.Title = txtTitle.Text;
            area.Description = txtDescription.Text;
            area.ModuleID = this.ModuleID;
            area.Insert();

            gvArea.DataBind();
            this.ResetInputData();
        }

        private static bool CheckRole(string roleID, string roleString)
        {
            foreach (string r in roleString.Split(';'))
            {
                if (roleID.Trim() == r.Trim())
                {
                    return true;
                }
            }
            return false;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
           if (IsPostBack == false)
           {
               // All Roles by PortalID.
               DataTable dtRoles = Role.SelectAllByPortalID(Global.PortalID).Tables[0];
               chkEditRoles.Items.Clear();
               foreach (DataRow row in dtRoles.Rows)
               {
                   ListItem item = new ListItem();
                   item.Text = row["Name"].ToString();
                   item.Value = row["ID"].ToString();
                   //if (CheckRole(item.Value, m.AuthorizedViewRoles))
                   //{
                   //    item.Selected = true;
                   //}
                   // Administrators Role.
                   if (item.Value == "1")
                   {
                       item.Selected = true;
                       item.Enabled = false;
                   }
                   chkEditRoles.Items.Add(item);
               }
           }
        }

        private void ResetInputData()
        {
            txtTitle.Text = "";
            txtDescription.Text = "";
            ViewState["AreaID"] = "";
            gvArea.SelectedIndex = -1;
        }
    }
}